﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

using Ccit.Utils;
using Ccit.OA.MsPersonDoc;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Ccit.OA.PersonDoc
{
    public partial class persondoc_file_upload : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                //获取文件夹ID
                ViewState["FolderId"] = (null != Session["folderId"]) ? Session["folderId"].ToString() : "1";
                ViewState["UserIdShare"] = (null != Session["UserIdShare"]) ? Session["UserIdShare"].ToString() : "";
                //btnConfirm.Attributes.Add("onclick", "return doupload('" + ViewState["Space"].ToString().Trim() + "')");
            }
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            string sMsg = string.Empty;
            string sFileName = string.Empty;

            string serAttach = string.Empty;
                if (doSave())
                {
                    Response.Write("<script>alert('文件上传成功!');</script>");
                }
                else
                {
                    sMsg = "操作失败,请重试!";
                }

        }


        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("persondoc_mydoc_frame.aspx");
        }


        private bool doSave()
        {
            string sFolderId = ViewState["FolderId"].ToString().Trim();
            int sGuid = 0;
            string cmdText = "";
            int iExecuteRow = 0;
            //获取文件对象
            HttpPostedFile oPf = MyFile.PostedFile;
            //获取大小
            int iContentLength = oPf.ContentLength;
            Byte[] oByte = new byte[iContentLength];

            #region 文件基础信息 插入
            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;
            if (ViewState["UserIdShare"].ToString().Trim() == "")
            {
                ViewState["UserIdShare"] = loginUser.UserId;
            }
            MsFiles Ms = new MsFiles();
            Ms.fileName = GetFileName("Full");
            Ms.fileType = Ms.fileName.Substring(Ms.fileName.LastIndexOf(".")+ 1).ToLower();
            Ms.folderId = sFolderId;
            Ms.filelength = iContentLength.ToString().Trim();
            Ms.remark = txtremark.Text.Trim();
            Ms.updatetime = System.DateTime.Now.ToString();
            Ms.userId = ViewState["UserIdShare"].ToString().Trim();
            Ms.jobId = loginUser.PosId;
            Ms.deptId = loginUser.DeptId;
            Ms.cjuserId = loginUser.UserId;
            Ms.levelId = loginUser.ClassId;
            Ms.agencId = loginUser.OrganId;
            Ms.cjdatetime = System.DateTime.Now.ToString();
            PersonDocBLL bll = new PersonDocBLL();
            Ms = bll.AddFile(Ms);
            #endregion
            //获取文件流
            Stream oStream = oPf.InputStream;
            oStream.Read(oByte, 0, iContentLength);
            sGuid = int.Parse(Ms.Id);
            cmdText = "INSERT INTO DOC_A4(DOC_A4_10,DOC_A4_20,DOC_A4_30) VALUES(#sGuid,#sVersion,#sContent)";

            IDataParameter guid = DataAccessor.GetDataParameter("sGuid", DataType.Int);
            IDataParameter version = DataAccessor.GetDataParameter("sVersion", DataType.Int);
            IDataParameter content = DataAccessor.GetDataParameter("sContent", DataType.Binary);

            guid.Value = sGuid;
            version.Value = 1;
            content.Value = oByte;

            IDataParameter[] parameters = new IDataParameter[] { guid, version, content };
            iExecuteRow = DataAccessor.ExecuteNonQuery(cmdText, parameters);

            Ms = null;
            if (iExecuteRow > 0)
            {
                ViewState["GUID"] = sGuid;
                ViewState["FileLength"] = iContentLength.ToString();
                return true;
            }
            else
                return false;
        }

        private string GetFileName(string sType)
        {
            HttpPostedFile oPf = MyFile.PostedFile;
            string sFileName = oPf.FileName;
            int iPostion = 0;

            iPostion = sFileName.LastIndexOf("\\");

            sFileName = sFileName.Substring((iPostion + 1));

            if (sType != "Full")   //不带扩展名的文件名
            {
                iPostion = sFileName.LastIndexOf(".");
                if (iPostion != -1)
                {
                    sFileName = sFileName.Substring(0, iPostion);
                }
            }
            return sFileName;
        }

    }
}
